export default (codes) => {
  const dictMap = ref({})
  const getDict = (code) => {
    return new Promise((resolve) => {
      uni
        .request({
          url: '/obj/enumTypeController/getEnum/' + code,
          method: 'GET',
          data: { parentCode: 'all' },
        })
        .then((res) => {
          resolve({ [code]: res.rows })
        })
        .catch(() => {
          resolve({ [code]: [] })
        })
    })
  }

  const apiGetDictList = codes.map((code) => getDict(code))
  Promise.all(apiGetDictList).then((res) => {
    dictMap.value = res.reduce((pre, next) => {
      return Object.assign(pre, next)
    }, {})
  })

  /**
   * getDictLabel
   * @param {number} code - 字典类型code
   * @param {number} value - 字典值
   * @returns {number} - 两个数字的和
   */
  const getDictLabel = (code, value) => {
    const dictData = dictMap.value[code] || []
    let curData = dictData.find((item) => item.dictValue === value)
    if (curData) {
      return curData.dictLabel
    } else {
      return ''
    }
  }
  return {
    dictMap,
    getDictLabel,
  }
}
